//
//  ShoppingTableViewCell.m
//  FruitVIP
//
//  Created by 孙艳玲 on 15/5/21.
//  Copyright (c) 2015年 孙艳玲. All rights reserved.
//

#import "ShoppingTableViewCell.h"

static NSInteger sumMoney;

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

@implementation ShoppingTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSub];
    }
    return self;
}

- (void)createSub
{
    self.count = 0;
    self.foodImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH / 3, SCREEN_WIDTH / 3)];
//    foodImageView.backgroundColor = [UIColor yellowColor];
    self.foodImageView.userInteractionEnabled = YES;
     UIImage *image = [UIImage imageNamed:@"detail.jpg"];
    self.foodImageView.image = image;
    self.foodImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:self.foodImageView];
    
    
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.foodImageView.frame.origin.x + self.foodImageView.frame.size.width + 5, self.foodImageView.frame.origin.y + 20, (SCREEN_WIDTH - self.foodImageView.frame.size.width - 15)/2.1, (self.foodImageView.frame.size.height - 10)/ 5 - 5)];
    nameLabel.text = @"智力黑提智力黑提智力黑提";
//    nameLabel.backgroundColor = [UIColor yellowColor];
    nameLabel.font = [UIFont systemFontOfSize:15];
    
    CGRect nameFrame = nameLabel.frame;
    nameFrame.size.height = [[self class] heightForName:nameLabel.text];
    nameLabel.frame = nameFrame;
    nameLabel.numberOfLines = 0;
    
    
    [self.contentView addSubview:nameLabel];
    
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(nameLabel.frame.origin.x, nameLabel.frame.origin.y + nameLabel.frame.size.height + 10, 20, self.foodImageView.frame.size.height / 4)];
    label.text = @"￥";
    label.font = [UIFont systemFontOfSize:20];
    label.textColor = [UIColor blackColor];
    [self.contentView addSubview:label];
    
    
    
    self.priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(label.frame.origin.x + label.frame.size.width + 1, label.frame.origin.y , nameLabel.frame.size.width - label.frame.size.width - 1, self.foodImageView.frame.size.height / 4)];
//    self.priceLabel.text = @"79.00元";
//    self.priceLabel.backgroundColor = [UIColor yellowColor];
    self.priceLabel.textColor = [UIColor blackColor];
    self.priceLabel.font = [UIFont systemFontOfSize:20];
    [self.contentView addSubview:self.priceLabel];

    self.cutBUtton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cutBUtton.frame = CGRectMake(nameLabel.frame.origin.x + nameLabel.frame.size.width + 5, nameLabel.frame.origin.y, nameLabel.frame.size.width/3.5, self.foodImageView.frame.size.height / 3);
    self.cutBUtton.backgroundColor = [UIColor colorWithRed:242.0 / 255.0 green:204.0 / 255.0 blue:49.0 / 255.0 alpha:1];
    self.cutBUtton.tag = 1002;
    [self.cutBUtton setTitle:@"-" forState:UIControlStateNormal];
    [self.cutBUtton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.cutBUtton addTarget:self action:@selector(cutButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.cutBUtton];
    
    self.countLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.cutBUtton.frame.origin.x + self.cutBUtton.frame.size.width , self.cutBUtton.frame.origin.y, self.foodImageView.frame.size.width / 3, self.cutBUtton.frame.size.height)];
    self.countLabel.text = [NSString stringWithFormat:@"%ld", self.count];
    [self.countLabel.layer setBorderWidth:1.0];
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 230/255.0, 230/255.0, 230/255.0, 1});
    [self.countLabel.layer setBorderColor:colorref];
    self.countLabel.font = [UIFont systemFontOfSize:17];
    self.countLabel.textAlignment = NSTextAlignmentCenter;
    self.countLabel.textColor = [UIColor blackColor];
    [self.contentView addSubview:self.countLabel];
    
    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    addButton.frame = CGRectMake(self.countLabel.frame.origin.x + self.countLabel.frame.size.width, self.countLabel.frame.origin.y, self.cutBUtton.frame.size.width, self.cutBUtton.frame.size.height);
    addButton.backgroundColor = [UIColor colorWithRed:242.0 / 255.0 green:204.0 / 255.0 blue:49.0 / 255.0 alpha:1];
    [addButton setTitle:@"+" forState:UIControlStateNormal];
    [addButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    addButton.tag = 1001;
    [addButton addTarget:self action:@selector(addButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:addButton];
    
    
    UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteButton.frame = CGRectMake(self.cutBUtton.frame.origin.x, self.cutBUtton.frame.origin.y + self.cutBUtton.frame.size.height + 10, self.cutBUtton.frame.size.width + self.countLabel.frame.size.width + addButton.frame.size.width, 2 * self.cutBUtton.frame.size.height/3);
    deleteButton.backgroundColor = [UIColor colorWithRed:109.0/255.0 green:77.0 / 255.0 blue:69 / 255.0 alpha:1];
    [deleteButton setTitle:@"删除" forState:UIControlStateNormal];
    [deleteButton setTitleColor:[UIColor colorWithRed:242.0 / 255.0 green:204.0 / 255.0 blue:49.0 / 255.0 alpha:1] forState:UIControlStateNormal];
    deleteButton.layer.cornerRadius = CGRectGetHeight([deleteButton bounds])/5;
    [deleteButton addTarget:self action:@selector(deleteButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:deleteButton];
    
}

-(void)cutButtonAction
{

    NSString *newString = self.countLabel.text ;
    NSInteger intString = [newString integerValue];
    if (intString > 1) {
        intString--;
        NSString *string = [NSString stringWithFormat:@"%ld", intString];
        self.countLabel.text = string;
        NSLog(@"%ld", intString);

    }
    else {
        return;
    }

    sumMoney = sumMoney - [self.priceLabel.text integerValue];
    [self.myDelegate allPrice:sumMoney];
    
       }
- (void)addButtonAction
{
    
    NSString *newString = self.countLabel.text ;
    //把self.countLabel.text字符串转化为整型
    NSInteger intString = [newString integerValue];
    
    intString++;
    //把整型转化为字符串
    NSString *string = [NSString stringWithFormat:@"%ld", intString];
    //self.countLabel.text赋值
    self.countLabel.text = string;
   
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults] ;
    [userDefault setObject:string forKey:self.countLabel.text];
    
    sumMoney = sumMoney + [self.priceLabel.text integerValue];
    [self.myDelegate allPrice:sumMoney];
    
}

- (void)awakeFromNib {
    // Initialization code
}

+ (CGFloat)heightForName:(NSString *)name
{
    CGSize size = CGSizeMake((SCREEN_WIDTH - SCREEN_WIDTH / 3 - 15)/2.1, 1000);
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:15] forKey:NSFontAttributeName];
    CGRect frame = [name boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return frame.size.height;

}

//删除按钮
- (void)deleteButtonAction
{
    [self.deleteDelegate deleteButton];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
